<?php
/**
 * 检查设备数据脚本
 */

// 检查IMEI是否存在
$imei = '863780074784151';

try {
    // 连接数据库
    $pdo = new PDO('mysql:host=8.155.50.137;port=3306;dbname=testdemo;charset=utf8', 'root', 'root');
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
    echo "=== 检查设备数据 ===\n";
    echo "IMEI: {$imei}\n\n";
    
    // 查询flow_wifi_imei表
    $stmt = $pdo->prepare("SELECT * FROM flow_wifi_imei WHERE IMEI = ?");
    $stmt->execute([$imei]);
    $device = $stmt->fetch(PDO::FETCH_ASSOC);
    
    if ($device) {
        echo "✅ 在flow_wifi_imei表中找到设备:\n";
        echo "ID: {$device['id']}\n";
        echo "IMEI: {$device['IMEI']}\n";
        echo "MAC: {$device['mac']}\n";
        echo "创建时间: {$device['createtime']}\n";
        echo "更新时间: {$device['updatetime']}\n\n";
    } else {
        echo "❌ 在flow_wifi_imei表中未找到设备\n\n";
        
        // 查看表中有哪些数据
        $stmt = $pdo->query("SELECT IMEI, mac FROM flow_wifi_imei LIMIT 5");
        $devices = $stmt->fetchAll(PDO::FETCH_ASSOC);
        
        echo "表中现有的设备数据（前5条）:\n";
        foreach ($devices as $d) {
            echo "  IMEI: {$d['IMEI']}, MAC: {$d['mac']}\n";
        }
        echo "\n";
        
        // 插入测试数据
        echo "正在插入测试设备数据...\n";
        $insertStmt = $pdo->prepare("INSERT INTO flow_wifi_imei (IMEI, mac, createtime, updatetime) VALUES (?, ?, NOW(), NOW())");
        $insertStmt->execute([$imei, '1C3426522542']);
        echo "✅ 测试设备数据插入成功\n\n";
    }
    
    // 再次查询确认
    $stmt = $pdo->prepare("SELECT * FROM flow_wifi_imei WHERE IMEI = ?");
    $stmt->execute([$imei]);
    $device = $stmt->fetch(PDO::FETCH_ASSOC);
    
    if ($device) {
        echo "✅ 最终确认设备存在:\n";
        echo "IMEI: {$device['IMEI']}\n";
        echo "MAC: {$device['mac']}\n";
    } else {
        echo "❌ 设备仍然不存在\n";
    }
    
} catch (Exception $e) {
    echo "❌ 数据库错误: " . $e->getMessage() . "\n";
}
?>
